package com.shxc.smarttrade.community.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.community.model.MasterSimpleModel;

import java.util.List;

/**
 * 社区 - 大师
 */

public class AndroidMasterAdapter extends RecyclerView.Adapter<AndroidMasterAdapter.ViewHolder> {

    private Context mContext;
    private List<MasterSimpleModel> mList;
    private Handler mHandler;

    public AndroidMasterAdapter(Context c, List<MasterSimpleModel> list, Handler handler) {
        mContext = c;
        mList = list;
        mHandler = handler;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_community_master, parent, false);
        ViewHolder holder = new ViewHolder(inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        final MasterSimpleModel model = mList.get(position);

        holder.imageview.setImageResource(model.getMipmapId());

        //跳转页面
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msg = new Message();
                msg.what = 1;
                msg.obj = model;
                mHandler.sendMessage(msg);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        protected ImageView imageview;

        public ViewHolder(View itemView) {
            super(itemView);
            imageview = itemView.findViewById(R.id.imageview);
        }
    }

}
